package com.blacksheep.mtm.service.Impl;

import com.blacksheep.mtm.common.exception.XmallException;
import com.blacksheep.mtm.dao.TbOrderItemMapper;
import com.blacksheep.mtm.dao.TbOrderReviewMapper;
import com.blacksheep.mtm.dao.TbUserMapper;
import com.blacksheep.mtm.dto.MedicineItemDto;
import com.blacksheep.mtm.dto.front.OrderFrontDto;
import com.blacksheep.mtm.dto.front.OrderReviewFront;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.manager.TbOrderReview;
import com.blacksheep.mtm.service.OrderReviewService;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderReviewServiceImpl implements OrderReviewService {

    @Autowired
    private TbOrderReviewMapper reviewMapper;
    @Autowired
    private TbOrderItemMapper orderItemMapper;

    @Autowired
    private TbUserMapper userMapper;
    @Override
    public DataTablesResult getReviewList(int draw, int start, int length, int[] state, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result = new DataTablesResult();
        List<OrderReviewFront> dtoList = new ArrayList<>();
        //分页
        PageHelper.startPage(start / length + 1, length);
        dtoList = reviewMapper.selectList("%" + search + "%", state, minDate, maxDate, orderCol, orderDir);
        result.setDraw(draw);
        result.setData(dtoList);
        return result;
    }

    @Override
    public TbOrderReview add(TbOrderReview review) {
        int result = reviewMapper.insertSelective(review);
        if (result< 0){
            throw  new XmallException("新增失败");
        }
        return review;
    }

    @Override
    public int alertState(long id, int state) {
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        Long userId = userMapper.getUserIdByUsername(username);
        TbOrderReview review = reviewMapper.selectByPrimaryKey(id);
        if (review!=null){
            review.setState(state);
            review.setOperator(userId);
            int result = reviewMapper.updateByPrimaryKeySelective(review);
            if (result <1){
                throw  new XmallException("修改失败");
            }
        }else{
            throw  new XmallException("修改失败，订单不存在");
        }
        return 0;
    }

}
